﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class MenuSection : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public Enumerations.DisplayMode? DisplayMode { get; set; }
        public string Id { get; set; }
        public string MaxHeight { get; set; }
        public bool? Scrollable { get; set; }
        public int? Sequence { get; set; }
        //public string Title { get; set; }
        public Controls Controls { get; set; }
        public Gallery Gallery { get; set; }

        public MenuSection()
        {
            // TODO Vérifier la construction des controls
            this.Controls = new Controls(true);
            this.Gallery = new Gallery();
        }

        public MenuSection(XmlNode node)
        {
            try
            {
                if(XmlHelper.GetAttribute(node, "DisplayMode").Length > 0)
                    this.DisplayMode = (Enumerations.DisplayMode)Enum.Parse(typeof(Enumerations.DisplayMode), XmlHelper.GetAttribute(node, "DisplayMode"));

                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.MaxHeight = XmlHelper.GetAttribute(node, "MaxHeight");
                this.Scrollable = XmlHelper.GetAttribute(node, "Scrollable") == "true";
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");

                if (node.SelectSingleNode("Controls") != null)
                {
                    this.Controls = new Controls(node.SelectSingleNode("Controls"), false);
                }
                else
                {
                    this.Controls = new Controls(true);
                }

                if (node.SelectSingleNode("Gallery") != null)
                {
                    this.Gallery = new Gallery(node.SelectSingleNode("Gallery"));
                }
                else
                {
                    this.Gallery = new Gallery();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode menuSectionNode = XmlHelper.AddNode(commandUINode, "MenuSection");
                XmlHelper.AddAttribute(menuSectionNode, "Id", this.Id);
                XmlHelper.AddAttribute(menuSectionNode, "MaxHeight", this.MaxHeight);
                XmlHelper.AddAttribute(menuSectionNode, "Scrollable", this.Scrollable);
                XmlHelper.AddAttribute(menuSectionNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(menuSectionNode, "DisplayMode", this.DisplayMode.Value.ToString());

                if (this.Controls != null)
                {
                    this.Controls.AddNode(menuSectionNode);
                }

                if (this.Gallery != null)
                {
                    this.Gallery.AddNode(menuSectionNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("MenuSection");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Controls", "Gallery" }, true);

                if (this.Controls != null)
                {
                    this.Controls.BuildTreeViewElement(node);
                }

                if (this.Gallery != null)
                {
                    this.Gallery.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Gallery) && !(child is Controls))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (child is Gallery)
                    {
                        this.Gallery = (Gallery)child;
                    }
                    else
                    {
                        this.Controls = (Controls)child;
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Controls)
                {
                    this.Controls = null;
                }
                else if (o is Gallery)
                {
                    this.Gallery = null;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
